import 'dart:math';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
import 'package:ls_work/res/gaps.dart';

import '../a_yyt/models/floor_list_entity.dart';
import '../a_yyt/models/medical_elder_entity.dart';
import '../res/colors.dart';
import '../viewmodel/select_old_view_model.dart';
import '../widgets/drop_item_widget.dart';

class SelectMulitOldPage extends BaseStatefulMvvmWidget {
  Function(List<MedicalElderInfo> old) selectListener;

  SelectMulitOldPage(this.selectListener);

  @override
  State<StatefulWidget> createState() => _SelectOldState();
}

class _SelectOldState
    extends BaseMvvmRefreshState<SelectOldViewModel, SelectMulitOldPage> {
  GZXDropdownMenuController _dropdownMenuController =
      GZXDropdownMenuController();

  GlobalKey _stackKey = GlobalKey();

  List<MedicalElderInfo> selectOldList = [];

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text("选择长者"),
      ),
      body: Stack(
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              GZXDropDownHeader(
                // 下拉的头部项，目前每一项，只能自定义显示的文字、图标、图标大小修改
                items: [
                  GZXDropDownHeaderItem(
                      viewModel.selectBuilding?.buildingNo ?? "全部"),
                  GZXDropDownHeaderItem(viewModel.selectFloor?.floorNo ?? "全部"),
                ],
                // GZXDropDownHeader对应第一父级Stack的key
                stackKey: _stackKey,
                // controller用于控制menu的显示或隐藏
                controller: _dropdownMenuController,
                height: 50,
                color: Colours.bg_color,
                borderColor: Colours.bg_color,
                dividerHeight: 20,
                dividerColor: Colours.order_line,
                style: TextStyle(color: Color(0xFF666666), fontSize: 14),
                dropDownStyle: TextStyle(
                  fontSize: 14,
                  color: Colours.app_main,
                ),
                iconDropDownColor: Colours.app_main,
              ),
              Container(
                color: Colours.bg_color,
                child: CommonInput(
                  hintText: "请输入房间号/姓名",
                  border: InputBorder.none,
                  isDense: true,
                  padding: EdgeInsets.zero,
                  textInputAction: TextInputAction.search,
                  onChanged: (str) {
                    viewModel.searchName = str;
                  },
                  needClear: true,
                  onSubmitted: (str) {
                    viewModel.requestRefresh();
                  },
                ).addToContainer(
                    margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                    decoration: BoxDecoration(
                        color: Colors.white, borderRadius: 30.borderRadius),
                    padding:
                        EdgeInsets.symmetric(horizontal: 10, vertical: 10)),
              ),
              loadingContentWidget.setWeight(1)
            ],
          ),

          // 下拉菜单
          GZXDropDownMenu(
            // controller用于控制menu的显示或隐藏
            controller: _dropdownMenuController,
            // 下拉菜单显示或隐藏动画时长
            animationMilliseconds: 300,
            dropdownMenuChanging: (isShow, index) {},
            dropdownMenuChanged: (isShow, index) {},
            menus: [
              if ((viewModel.buildingList ?? []).isNotEmpty)
                GZXDropdownMenuBuilder(
                  dropDownHeight: min(
                      40.0 * (viewModel.buildingList ?? []).length,
                      MediaQuery.of(context).size.height * 0.75),
                  dropDownWidget: buildConditionListWidget(
                      viewModel.buildingList ?? [], (data) {
                    _dropdownMenuController.hide();
                    setState(() {
                      viewModel.selectBuilding = data as FloorEntity?;
                      viewModel.setFloor();
                    });
                    viewModel.requestRefresh();
                  }),
                ),
              if ((viewModel.floorList ?? []).isNotEmpty)
                GZXDropdownMenuBuilder(
                  dropDownHeight: min(40.0 * (viewModel.floorList ?? []).length,
                      MediaQuery.of(context).size.height * 0.75),
                  dropDownWidget: buildConditionListWidget(
                      viewModel.floorList ?? [], (value) {
                    _dropdownMenuController.hide();
                    setState(() {
                      viewModel.selectFloor = value as FloorInfo?;
                    });
                    viewModel.requestRefresh();
                  }),
                ),
            ],
          ),

          if(selectOldList.isNotEmpty)
          TextButton(
                  onPressed: () {
                    widget.selectListener(selectOldList);
                    Go().pop();
                  },
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Colours.app_main)
              ),
                  child:
                      CommonText('确认', textSize: 17, textColor: Colors.white))
              .setLocation(left: 16, right: 16, bottom: 12)
        ],
        key: _stackKey,
      ),
    );
  }

  @override
  Widget createScrollWidget() {
    return ListView.separated(
        padding: EdgeInsets.only(bottom: 70),
        itemBuilder: (context, index) {
          var item = viewModel.listItems[index];
          return Row(
            children: [
              CommonText(item.name).setWeight(1),
              CommonText("${item.floorNo}#${item.roomNo}").setWeight(2),
              Icon(
                selectOldList.contains(item)
                    ? Icons.check_box_rounded
                    : Icons.check_box_outline_blank_outlined,
                color: selectOldList.contains(item)
                    ? Colours.app_main
                    : Colors.grey,
              )
            ],
          ).onTap(() {
            setState(() {
              if (selectOldList.contains(item)) {
                selectOldList.remove(item);
              } else {
                selectOldList.add(item);
              }
            });
          }).addToContainer(
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
              color: Colors.white);
        },
        separatorBuilder: (context, index) {
          return Gaps.dividerLine;
        },
        itemCount: viewModel.listItems.length);
  }

  @override
  SelectOldViewModel createViewModel() {
    return SelectOldViewModel(context);
  }

  @override
  bool get canPullUp => true;
}
